package com.jp.knowledge.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.OrganizeInfoModel;
import com.jp.knowledge.util.HeadIconUploader;
import com.jp.knowledge.util.JsonUtil;
import com.jp.knowledge.util.MultipleImageUploader;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/8/15.
 */

public class SaveOrganizeInfoService extends Service implements MultipleImageUploader.ImageUploadCallBack, ResultSubscriber.OnResultListener {

	private OrganizeInfoModel changeModel;
	private OrganizeInfoModel organizeInfoModel;
	private HeadIconUploader headIconUploader;

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		headIconUploader = new HeadIconUploader(this);
		headIconUploader.setImageUploadCallBack(this);
		changeModel = (OrganizeInfoModel) intent.getSerializableExtra("data");
		organizeInfoModel = (OrganizeInfoModel) intent.getSerializableExtra("originalData");
		if(changeModel == null)
			stopSelf();
		if(changeModel.getCompanyicon() != null)
			commitIcon();
		else
			commitUserData();
		return super.onStartCommand(intent, flags, startId);
	}

	private void commitUserData(){
		if(changeModel.getCompanyicon() == null
				&& changeModel.getCompanyname() == null
				&& changeModel.getCompanyphone() == null
				&& changeModel.getCompanyhttp() == null
				&& changeModel.getEmail() == null
				&& changeModel.getCity() == null
				&& changeModel.getArea() == null
				&& changeModel.getProvince() == null
				&& changeModel.getAddress() == null
				&& changeModel.getEmployeesnum() == null
				&& changeModel.getKnowledge() == null){
			stopSelf();
			return;
		}

		JsonObject param = new JsonObject();
		param.addProperty("companyid", changeModel.getCompanyid());
		param.addProperty("companytype", changeModel.getCompanytype());
		param.addProperty("companyicon", changeModel.getCompanyicon());
		param.addProperty("companyname", changeModel.getCompanyname());
		param.addProperty("companyphone", changeModel.getCompanyphone());
		param.addProperty("companyhttp", changeModel.getCompanyhttp());
		param.addProperty("email", changeModel.getEmail());
		param.addProperty("city", changeModel.getCity());
		param.addProperty("area", changeModel.getArea());
		param.addProperty("province", changeModel.getProvince());
		param.addProperty("address", changeModel.getAddress());

		if(changeModel.getKnowledge() != null){
			JsonArray addKnowledge = new JsonArray();
			JsonArray delKnowledge = new JsonArray();
			if(organizeInfoModel == null || organizeInfoModel.getKnowledge() == null){
				for(OrganizeInfoModel.KnowledgeBean knowledgeBean : changeModel.getKnowledge())
					addKnowledge.add(knowledgeBean.getCateId());
			} else{
				List<OrganizeInfoModel.KnowledgeBean> changeKnowledgeBeen = changeModel.getKnowledge();
				List<OrganizeInfoModel.KnowledgeBean> organizeKnowledgeBeen = organizeInfoModel.getKnowledge();
				for(OrganizeInfoModel.KnowledgeBean knowledgeBean : changeKnowledgeBeen)
					if(!knowledgeContent(knowledgeBean, organizeKnowledgeBeen))
						addKnowledge.add(knowledgeBean.getCateId());
				for(OrganizeInfoModel.KnowledgeBean knowledgeBean : organizeKnowledgeBeen)
					if(!knowledgeContent(knowledgeBean, changeKnowledgeBeen))
						delKnowledge.add(knowledgeBean.getCateId());
			}
			if(addKnowledge.size() > 0)
				param.add("addKnowledge", addKnowledge);
			if(delKnowledge.size() > 0)
				param.add("delKnowledge", delKnowledge);
		}

		try {
			param.addProperty("employeesnum", Integer.parseInt(changeModel.getEmployeesnum()));
		} catch (Exception e){} finally {
			HttpHelper.getInstance(this).organizaEdit(param, 0x01, this);
		}
	}

	private boolean knowledgeContent(OrganizeInfoModel.KnowledgeBean knowledgeBean, List<OrganizeInfoModel.KnowledgeBean> knowledgeBeen){
		if(knowledgeBean == null || knowledgeBean.getCateId() == null || knowledgeBeen == null)
			return false;
		String cateId = knowledgeBean.getCateId();
		for(OrganizeInfoModel.KnowledgeBean bean : knowledgeBeen)
			if(cateId.equals(bean.getCateId()))
				return true;
		return false;
	}

	private void commitIcon(){
		MultipleImageUploader.ImageBean imageBean = new MultipleImageUploader.ImageBean();
		imageBean.setPath(changeModel.getCompanyicon());
		String key = String.format("library/company/%s/icon", changeModel.getCompanyid());
		imageBean.setKey(key);
		headIconUploader.addImage(imageBean);
		headIconUploader.uploadImages();
	}

	@Nullable
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void imageCompressFail(String key) {
		imageUploadSuccess(key, null);
	}
	@Override
	public void imageCompressSuccess(String key) {}
	@Override
	public void imageCompressComplete() {}
	@Override
	public void imageUploadFail(String key) {
		imageUploadSuccess(key, null);
	}
	@Override
	public void imageUploadSuccess(String key, String resultKey) {
		changeModel.setCompanyicon(resultKey);
		commitUserData();
	}
	@Override
	public void imageUploadComplete(List<String> resultKeys) {}

	@Override
	public void onStart(int requestType) {}
	@Override
	public void onCompleted(int requestType) {
		stopSelf();
	}
	@Override
	public void onError(int requestType) {
		onCompleted(requestType);
	}
	@Override
	public void onNext(IModel data, int requestType) {
		onCompleted(requestType);
	}
}
